package top.techqi.hamtools

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.content.Intent
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_boot.*
import top.techqi.droidbdf.pangu.base.BaseActivity
import top.techqi.hamtools.drawable.SweepDrawable
import java.util.*

class BootActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_boot)
    }

    override fun onContentChanged() {
        super.onContentChanged()
        border.setImageResource(R.drawable.boot_border)

        center.setImageResource(R.drawable.boot_center)

        circle.setImageDrawable(SweepDrawable(resources.getDrawable(R.drawable.boot_circle), 135f, 405f))
        circle.setImageLevel(0)
    }

    override fun onStart() {
        super.onStart()
        with(ObjectAnimator.ofPropertyValuesHolder(border,
                PropertyValuesHolder.ofFloat("scaleX", 0.3f, 1f),
                PropertyValuesHolder.ofFloat("scaleY", 0.3f, 1f))) {
            setDuration(1500).start()
        }
        with(ObjectAnimator.ofFloat(center, "alpha", 0f, 1f)) {
            setDuration(2000).start()
        }
        with(ObjectAnimator.ofInt(circle, "imageLevel", 0, 10000)) {
            interpolator = null
            startDelay = 1500
            setDuration(1000).start()
        }
        with(ObjectAnimator.ofInt(0, getString(R.string.app_name).length)) {
            addUpdateListener {
                label.text = getString(R.string.app_name).subSequence(0, it.animatedValue as Int)
            }
            startDelay = 2000
            setDuration(500).start()
        }

        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                startActivity(Intent(this@BootActivity, MainActivity::class.java))
                finish()
            }
        }, 3000)
    }
}
